/**
 * Created by wenchuan on 2018/5/9.
 */
define(function () {

    var self,
        app = win.app,
        others = {
            ischecked: '0',
            countAmount: 0
        },
        subOrders = [],
        defaultinfo = {},
        pageParameters
    ;

    function setCountAmount() {
        var c = 0;
        self._getRefreshObj().data.forEach(function (v) {
            if (v.ischecked == '1') {
                c += (Number(v.GWCQ004) * Number(v.GWCQ003));
            }
        });
        others.countAmount = c;
    }

    var OrderTemp = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _limit: 20,
                _refreshAutoInit: true //自动加载刷新控件
            }, win);
        },
        subData: function () {
            app.openPage({
                src: "gwcxd/gwc-submit.html",
                argsKey: 'gwcsubmit_page',
                args: {
                    items: subOrders,
                    defaultinfo:defaultinfo,
                    cb: function () {
                        self._refresh();
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll1'];
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat300/getGwcqData",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        defaultinfo = result.data.data[0];
                        goods.data.pushArray(datas.addMap({
                            ischecked: '1'
                        }), goods.page == 0);

                        _super({
                            obj: goods,
                            dataLen: datas.length,
                            nodata: '购物车无商品...'
                        });

                        setCountAmount();
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        subOrders.pushArray(self._getRefreshObj().data.filterKV({
                            ischecked: '1'
                        }), true);
                        if (subOrders.length > 0) {
                            app.confirm({
                                msg: '确定是否提交？',
                                ok: function (e) {
                                    self.subData();
                                }
                            });
                        }
                    },
                    addOrder: function () {
                        app.openPage({
                            src: "gwcxd/classes.html",
                            argsKey: 'classes_page',
                            args: {
                                _targer: 'gwc-add',
                                cb: function (order) {
                                    //subOrders.push(order);
                                    self._refresh();
                                    app.back(2);
                                }
                            }
                        });
                    },
                    checkChange: function () {
                        self._getRefreshObj().data.addMap({
                            ischecked: others.ischecked
                        }, true);
                    }
                }
            });
        },
        _ready: function () {
            Vue.component('gwc-listitem', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                watch: {
                    'model': {
                        handler: function (val, oldVal) {
                            setCountAmount();
                        },
                        deep: true
                    }
                },
                methods: {
                    minus: function (val) {
                        if (val.GWCQ003 > 1) {
                            val.GWCQ003--;
                        }
                    },
                    plus: function (val) {
                        val.GWCQ003++;
                    },
                    showDetail: function (val) {
                        val.gwap100_1 = val.GWCQ004;
                        app.openPage({
                            src: "gwcxd/classes-info.html",
                            argsKey: 'classesinfo_page',
                            args: {
                                type: 'gwc-update',
                                item: val,
                                cb: function () {

                                }
                            }
                        });
                    },
                    deleteDetail: function(val, elobj){
                        var vue = this;
                        app.confirm({
                            msg: '是否删除？',
                            loading:true,
                            ok: function () {
                                app.ajax({
                                    xurl: "api/wx/gwat300/delete",
                                    xdata: val,
                                    cb: function (result) {
                                        if (result.ok) {
                                            self._getRefreshObj().data.remove(val);
                                            mui.swipeoutClose(Zepto(elobj.target).parents("li").get(0));
                                        }
                                    }
                                });
                            }
                        });
                    }
                }
            });
        }
    });

    return new OrderTemp();

});